package edu.stanford.prpl.app.prpltalk;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.UUID;

/**
 * TODO: Complete this class. For demo, this is stripped down to bare minimum
 */
public class Message {

	public static final String DATE_FORMAT = "EEE MMM dd HH:mm:ss zzz yyyy";

	String id;
	String from;
	String to;
	String subject;
	String text;
	String description;
	Date sentDate;

	public Message() {

		this.id = UUID.randomUUID().toString();
	}

	public Message(InputStream in) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String line;

		while ((line = br.readLine()) != null) {
			StringTokenizer st = new StringTokenizer(line);

			if (line.startsWith("From "))
				;
			else if (line.startsWith("From: "))
				this.from = line.substring(6);
			else if (line.startsWith("Date: ")) {
				String date = line.substring(6);
				SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
				try {
					this.sentDate = df.parse(date);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else if (line.startsWith("From: "))
				this.from = line.substring(6);
			else if (line.startsWith("Subject: "))
				this.subject = line.substring(9);
			else if (line.startsWith("Message-ID: "))
				this.id = line.substring(12);
			else if (line.startsWith("Content-Description: "))
				this.description = line.substring(21);
			else if (line.startsWith("MIME-Version: ") || line.startsWith("Content-Type: ")
					|| line.startsWith("Content-Transfer-Encoding: ") || line.startsWith(": "))
				;
			else {
				if (this.text == null)
					this.text = new String();
				this.text += line + "\n";
			}
		}
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getSentDate() {
		return sentDate;
	}

	public void setSentDate(Date sentDate) {
		this.sentDate = sentDate;
	}

	public String getContent() {
		return this.text;
	}

	public String toString() {
		// Calendar cal = Calendar.getInstance();
		StringBuffer sb = new StringBuffer();
		// sb.append("From "+this.getFrom() + " " + cal.getTime().toString());
		// sb.append("\n");
		if (this.sentDate != null) {
			SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
			sb.append("Date: " + df.format(this.sentDate));
		} else {
			sb.append("Date: ");
		}
		sb.append("\n");
		sb.append("From: " + this.getFrom());
		sb.append("\n");
		sb.append("Message-ID: " + this.id);
		sb.append("\n");
		sb.append("Subject: " + this.subject);
		sb.append("\n");
		sb.append("MIME-Version: 1.0");
		sb.append("\n");
		sb.append("Content-Type: text/plain; charset=us-ascii");
		sb.append("\n");
		sb.append("Content-Transfer-Encoding: 7bit");
		sb.append("\n");
		sb.append("Content-Description: " + this.description);
		// sb.append("\n");
		sb.append("\n");
		sb.append(this.text);
		sb.append("\n");
		// sb.append("\n");
		return sb.toString();
	}
}
